define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'shop_user/index' + location.search,
                    add_url: 'shop_user/add',
                    // edit_url: 'shop_user/edit',
                    del_url: 'shop_user/del',
                    multi_url: 'shop_user/multi',
                    import_url: 'shop_user/import',
                    table: 'shop',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                queryParams: function (params) {
                    var filter = JSON.parse(params.filter);
                    var op = JSON.parse(params.op);
                    var id = Fast.api.query('ids');
                    if (id != null) {
                        filter['shop_id'] = Fast.api.query('ids');
                        op['shop_id'] = '=';
                    }
                    params.filter = JSON.stringify(filter)
                    params.op = JSON.stringify(op)
                    return params;
                },
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('用户ID')},
                        {field: 'nickname', title: __('Title'), operate: 'LIKE'},
                        {field: 'mobile', title: __('手机号'), operate: 'LIKE'},
                        {field: 'shop.title', title: __('门店名称'), operate: 'LIKE'},
                        // {field: 'shop.description', title: __('Description'), operate: 'LIKE'},
                        // {field: 'notice', title: __('Notice'), operate: 'LIKE'},
                        // {field: 'province', title: __('Province'), operate: 'LIKE'},
                        // {field: 'city', title: __('City'), operate: 'LIKE'},
                        // {field: 'area', title: __('Area'), operate: 'LIKE'},
                        // {field: 'shop.name', title: __('Name'), operate: 'LIKE'},
                        // {field: 'shop.mobile', title: __('Mobile'), operate: 'LIKE'},
                        // {field: 'shop.workrange', title: __('Workrange'), operate: 'LIKE'},
                        // {field: 'shop.address', title: __('Address'), operate: 'LIKE'},
                        // {field: 'order_num', title: __('今日订单量')},
                        // {field: 'moneyCount', title: __('今日收入')},
                        {field: 'gender', title: __('是否为主账号'), searchList: {"0":__('否'),"1":__('是')}, formatter: Table.api.formatter.status},
                        // {field: 'status', title: __('Status'), searchList: {"normal":__('Status normal'),"hidden":__('Status hidden')}, formatter: Table.api.formatter.status},
                        {field: 'state', title: __('审核状态'), searchList: {"0":__('审核中'),"1":__('审核成功'),"2":__('审核失败')}, formatter: Table.api.formatter.status},
                        // {field: 'lng', title: __('Lng'), operate: 'LIKE'},
                        // {field: 'lat', title: __('Lat'), operate: 'LIKE'},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate,
                        buttons: [
                            // {
                            //     name: 'print',
                            //     title: '明细',
                            //     text: '明细',
                            //     classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                            //     url: 'user/mony_log/index',//接口地址 控制器名/方法名
                            // },
                            {
                                name: 'set_account',
                                title: '设置主账号',
                                text: '设置主账号',
                                classname: 'btn btn-primary btn-xs btn-view btn-ajax',
                                url: 'user/user/set_account',//接口地址 控制器名/方法名
                                success: function (data,rest) {
                                    table.bootstrapTable('refresh');

                                    //刷新table表单
                                    return true;
                                }
                            },
                            // {
                            //     name: 'update',
                            //     title: '审核',
                            //     text: '审核',
                            //     classname: 'btn btn-primary btn-xs btn-view btn-dialog',
                            //     url: 'shop_user/update',//接口地址 控制器名/方法名
                            //     visible: function (row) {//判断显示隐藏 只有状态为待审核时展示操作按钮
                            //         if (row.state != '1') {
                            //             return true;//显示
                            //         } else {
                            //             return false;//隐藏
                            //         }

                            //     },
                            // },
                        ]}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        recyclebin: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    'dragsort_url': ''
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: 'shop/recyclebin' + location.search,
                pk: 'id',
                sortName: 'id',
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'title', title: __('Title'), align: 'left'},
                        {
                            field: 'deletetime',
                            title: __('Deletetime'),
                            operate: 'RANGE',
                            addclass: 'datetimerange',
                            formatter: Table.api.formatter.datetime
                        },
                        {
                            field: 'operate',
                            width: '130px',
                            title: __('Operate'),
                            table: table,
                            events: Table.api.events.operate,
                            buttons: [
                                {
                                    name: 'Restore',
                                    text: __('Restore'),
                                    classname: 'btn btn-xs btn-info btn-ajax btn-restoreit',
                                    icon: 'fa fa-rotate-left',
                                    url: 'shop/restore',
                                    refresh: true
                                },
                                {
                                    name: 'Destroy',
                                    text: __('Destroy'),
                                    classname: 'btn btn-xs btn-danger btn-ajax btn-destroyit',
                                    icon: 'fa fa-times',
                                    url: 'shop/destroy',
                                    refresh: true
                                }
                            ],
                            formatter: Table.api.formatter.operate
                        }
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },

        add: function () {
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        update: function () {
            Controller.api.bindevent();
        },
        set_account: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
